<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MyPaging {
    private $main_url;
    private $url_suffix;
    private $count;
    private $limit;
    private $cur_offset;
    private $cur_page_offset_count;
    private $last_page_count;

    public function __construct() {}

    public function getCount  ()            { return $this->count; }
    public function getLimit  ()            { return $this->limit; }
    public function getMainURL()            { return $this->main_url; }
    public function getSuffix ()            { return $this->url_suffix; }
    public function getCurPageOffsetCount() { return $this->cur_page_offset_count; }
    public function getCurOffset()          { return $this->cur_offset; }
    public function setCount  ($value) { $this->count      = $value; }
    public function setLimit  ($value) { $this->limit      = $value; }
    public function setMainURL($value) { $this->main_url   = $value; }
    public function setSuffix ($value) { $this->url_suffix = $value; }

    public function getBar($page, $options = array()) {
        if($this->count == 0){
            return '';
        }

        $li = '';
        $visible_page_count = ($this->count) / ($this->limit);
        if($visible_page_count < 1){
            return '<ul class="pagination"><li class="disabled"><a href="#"><strong>'.$this->count.' / '.$this->count.'</strong></a></li></ul>';
        }
        if (($this->count) % ($this->limit) != 0) {
            $visible_page_count = floor($visible_page_count);
            $visible_page_count++;
        }
        if(count($options)>0){
            foreach ($options as $key => $value) {
                if($value==false){
                    unset($options[$key]);
                }
            }
        }

        //query
        $link_query = count($options)>0 ? '?' . http_build_query($options, '', '&amp;'):'';
        $q = $_SERVER['QUERY_STRING']!=''?'?'.$_SERVER['QUERY_STRING']:'';
        $link_query .= $q;

        //set prefix suffix
        $ul_prefix = '';
        $ul_suffix = '';
        $ul_prefix_href = $this->main_url . '/' . ($page - 1) . $this->url_suffix . $link_query;
        $ul_suffix_href = $this->main_url . '/' . ($page + 1) . $this->url_suffix . $link_query;
        if ($page == 1 || $page > $visible_page_count) {
            $ul_prefix = '<li class="disabled"><a href="#"><i class="fa fa-angle-left"></i></a></li>';
        } else {
            $ul_prefix = "<li><a href='{$ul_prefix_href}'><i class='fa fa-angle-left'></i></a></li>";
        }
        if ($page >= $visible_page_count) {
            $ul_suffix = '<li class="disabled"><a href="#"><i class="fa fa-angle-right"></i></a></li>';
        } else {
            $ul_suffix = "<li><a href='{$ul_suffix_href}'><i class='fa fa-angle-right'></i></li>";
        }

        //set li
        for ($i = 1; $i < $visible_page_count + 1; $i++) {
            $href = '';
            $li_class = ($page == $i ? 'active' : '');
            if ($li_class == 'active') {
                $href = '#';
            } else {
                $href = $this->main_url . '/' . $i . $this->url_suffix . $link_query;
            }
            $li .= "<li class='{$li_class}'><a href='{$href}'>{$i}</a></li>";
        }

        //set last page count
        $this->last_page_count = 0;
        if(($this->count) % ($this->limit) == 0 || $page < $visible_page_count){
            $this->last_page_count = ($page)*($this->limit);
            $this->cur_page_offset_count = $this->last_page_count;
        } else {
            $this->last_page_count = $this->count;
            $this->cur_page_offset_count = $this->count - $this->limit;
        }
        $this->cur_offset = ($page-1) * $this->limit + 1;

        //set li heading
        $li_heading = "<li class='disabled'><a href='#'><strong>" . $this->last_page_count . '</strong> / <strong>' . $this->count . "</strong></a></li>";

            // set fast link to 2 heading
        $li_first = $this->main_url . '/' . 1                   . $this->url_suffix . $link_query;
        $li_last  = $this->main_url . '/' . $visible_page_count . $this->url_suffix . $link_query;
        if($page == 1){
            $li_first = '<li class="disabled"><a href="#"><i class="fa fa-angle-double-left"></i></a></li>';
        } else {
            $li_first = '<li><a href="' . $li_first . '"><i class="fa fa-angle-double-left"></i></a></li>';
        }
        if($page == $visible_page_count) {
            $li_last = '<li class="disabled"><a href=#><i class="fa fa-angle-double-right"></i></a></li>';
        } else {
            $li_last = '<li><a href="' . $li_last . '"><i class="fa fa-angle-double-right"></i></a></li>';
        }

        return '<ul class="pagination">' . $li_heading . $li_first . $ul_prefix . $li . $ul_suffix . $li_last . '</ul>';
    }

    public function getSinglePagerBar($prev_url, $prev_title, $next_url, $next_title, $prev_text=null, $next_text=null, $pager_class=null) {
        $ul_prefix = "";
        $ul_suffix = "";
        if($next_text==null){ $next_text = $next_title; }
        if($prev_text==null){ $prev_text = $prev_title; }
        if ($prev_url != null) {
            $ul_prefix = "<li class='pull-right mgr5'><a href='{$prev_url}' title='{$prev_title}'>&larr;&nbsp;{$prev_text}</a></li>";
        }
        if ($next_url != null) {
            $ul_suffix = "<li class='pull-right'><a href='{$next_url}' title='{$next_title}'>{$next_text}&nbsp;&rarr;</a></li>";
        }
        return '<ul class="pager '. $pager_class .'">' . $ul_suffix . $ul_prefix . '</ul>';
    }
}
?>